#include <algorithm/analytical_model/io/json/converter/shared/utils/Vec3.h>
#include <utils/HasValue.h>

using namespace solar;

void solar::tag_invoke(const boost::json::value_from_tag& /*unused*/,
                       boost::json::value& json_value, const Vec3& vec)
{
    auto data = vec.data();
    json_value = {
        {"x", data[0]},
        {"y", data[1]},
        {"z", data[2]}
    };
}
auto solar::tag_invoke(const boost::json::value_to_tag<Vec3>& /*unused*/,
                       const boost::json::value& json_value) -> Vec3
{
    const auto* value_ptr = json_value.if_object();
    if (value_ptr == nullptr)
    {
        return {kUndefined, kUndefined, kUndefined};
    }
    double x = kUndefined;
    double y = kUndefined;
    double z = kUndefined;
    if (const auto* x_value = value_ptr->if_contains("x"); x_value != nullptr)
    {
        x = x_value->to_number<double>();
    }
    if (const auto* y_value = value_ptr->if_contains("y"); y_value != nullptr)
    {
        y = y_value->to_number<double>();
    }
    if (const auto* z_value = value_ptr->if_contains("z"); z_value != nullptr)
    {
        z = z_value->to_number<double>();
    }
    return {x, y, z};
}
